﻿<template>
	<div class="card-fill layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div>
				<el-form ref="formRefEquipmentSerial" :model="stateEquipmentSerial.vmModel" label-width="100px">
					<el-row>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_EquipmentId_FormItem"
								prop="Entity.EquipmentId"
								:label="$t('message.autotrans._Model_EquipmentSerial_Equipment')"
							>
								<el-select v-model="stateEquipmentSerial.vmModel.Entity.EquipmentId" @change="onEquipmentChange" clearable>
									<el-option v-for="item in stateEquipmentSerial.AllEquipments" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_TagId_FormItem" prop="Entity.TagId" :label="$t('message.autotrans._Model_EquipmentAlarmItem_EquipTag')">
								<el-select v-model="stateEquipmentSerial.vmModel.Entity.TagId" @change="onEquipmentTagChange" clearable>
									<el-option v-for="item in stateEquipmentSerial.AllEquipTags" :key="item.Value" :value="item.Value" :label="item.Text"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_SerialName_FormItem"
								prop="Entity.SerialName"
								:label="$t('message.autotrans._Model_EquipmentSerial_SerialName')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_SerialName') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input v-model="stateEquipmentSerial.vmModel.Entity.SerialName" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_Description_FormItem"
								prop="Entity.Description"
								:label="$t('message.autotrans._Model_EquipmentSerial_Description')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_Description') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input v-model="stateEquipmentSerial.vmModel.Entity.Description" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_BaudRate_FormItem"
								prop="Entity.BaudRate"
								:label="$t('message.autotrans._Model_EquipmentSerial_BaudRate')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_BaudRate') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input-number v-model="stateEquipmentSerial.vmModel.Entity.BaudRate" clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_DataLength_FormItem"
								prop="Entity.DataLength"
								:label="$t('message.autotrans._Model_EquipmentSerial_DataLength')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_DataLength') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input-number v-model="stateEquipmentSerial.vmModel.Entity.DataLength" clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_StopBit_FormItem"
								prop="Entity.StopBit"
								:label="$t('message.autotrans._Model_EquipmentSerial_StopBit')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_StopBit') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input-number v-model="stateEquipmentSerial.vmModel.Entity.StopBit" clearable></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_Parity_FormItem"
								prop="Entity.Parity"
								:label="$t('message.autotrans._Model_EquipmentSerial_Parity')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_Parity') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input-number v-model="stateEquipmentSerial.vmModel.Entity.Parity" clearable> </el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_Threshold_FormItem"
								prop="Entity.Threshold"
								:label="$t('message.autotrans._Model_EquipmentSerial_Threshold')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_Threshold') }),
										trigger: 'blur',
									},
								]"
							>
								<el-input-number v-model="stateEquipmentSerial.vmModel.Entity.Threshold" clearable></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_Active_FormItem"
								prop="Entity.Active"
								:label="$t('message.autotrans._Model_EquipmentSerial_Active')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_Active') }),
										trigger: 'blur',
									},
								]"
							>
								<el-switch v-model="stateEquipmentSerial.vmModel.Entity.Active" clearable></el-switch>
							</el-form-item>
						</el-col>
						<!--        <el-col :xs="24" :lg="24" class="mb20">-->
						<!--        <el-form-item ref="Entity_TagId_FormItem" prop="Entity.TagId" :label="$t('message.autotrans._Model_EquipmentSerial_TagId')">-->
						<!--            <el-input v-model="stateEquipmentSerial.vmModel.Entity.TagId" clearable></el-input>-->
						<!--        </el-form-item>-->
						<!--        </el-col>-->
						<!--        <el-form-item ref="Entity_TagName_FormItem" prop="Entity.TagName" :label="$t('message.autotrans._Model_EquipmentSerial_TagName')">-->
						<!--            <el-input v-model="stateEquipmentSerial.vmModel.Entity.TagName" clearable></el-input>-->
						<!--        </el-form-item>-->

						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item
								ref="Entity_GlobalPushlish_FormItem"
								prop="Entity.GlobalPushlish"
								:label="$t('message.autotrans._Model_EquipmentSerial_GlobalPushlish')"
								:rules="[
									{
										required: true,
										message: $t('message._system.common.vm.input', { input: $t('message.autotrans._Model_EquipmentSerial_GlobalPushlish') }),
										trigger: 'blur',
									},
								]"
							>
								<el-switch v-model="stateEquipmentSerial.vmModel.Entity.GlobalPushlish" clearable></el-switch>
							</el-form-item>
						</el-col>
					</el-row>

					<div style="text-align: right">
						<WtmButton @click="onSubmitEquipmentSerial" type="primary" :button-text="$t('message.autotrans.SysSubmit')" style="margin-top: 15px" />

						<WtmButton @click="onCloseEquipmentSerial" type="primary" :button-text="$t('message.autotrans.SysClose')" style="margin-top: 15px" />
					</div>
				</el-form>
			</div>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message.autotrans._PageEquipmentManagementEquipmentSerialCreate;false">
import { ElMessageBox, ElMessage, ElLoading } from 'element-plus';
import { defineAsyncComponent, reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { equipmentserialApi } from '/@/api/equipmentmanagement/equipmentserial';
import other from '/@/utils/other';
import fileApi from '/@/api/file';
import { useRouter } from 'vue-router';
const ci = getCurrentInstance() as any;

// 定义变量内容
const formRefEquipmentSerial = ref();
const stateEquipmentSerial = reactive({
	vmModel: {
		Entity: {
			EquipmentId: null,
			SerialName: '',
			Description: '',
			BaudRate: 9600,
			DataLength: 8,
			StopBit: 1,
			Parity: 0,
			Threshold: 1,
			Active: true,
			TagId: null,
			TagName: '',
			GlobalPushlish: false,
		},
	},

	AllEquipTags: [] as any[],
	AllEquipments: [] as any[],
});

// 取消
const onCloseEquipmentSerial = () => {
	closeDialog();
};

// 提交
const onSubmitEquipmentSerial = () => {
	const loadingInstance = ElLoading.service({
		lock: true,
		text: '正在提交中，请等待。。。',
		background: 'rgba(0, 0, 0, 0.7)',
	});
	formRefEquipmentSerial.value?.validate((valid: boolean, fields: any) => {
		if (valid) {
			equipmentserialApi()
				.add(stateEquipmentSerial.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loadingInstance.close();
				});
		} else {
			loadingInstance.close();
		}
	});
};

//选择设备时
const onEquipmentChange = () => {
	const equipmentId = stateEquipmentSerial.vmModel.Entity.EquipmentId;

	if (equipmentId) {
		// 父级有选中值：加载该设备对应的标签列表
		other.getSelectList(`/api/EquipmentManagement/EquipmentSerial/GetByEquipmentStringTags?id=${equipmentId}`, [], false).then((tags) => {
			// 更新子级选项：仅显示当前设备的标签
			stateEquipmentSerial.AllEquipTags = tags;
		});
		//获取设备名称
		const selectequip = stateEquipmentSerial.AllEquipments.find((item) => item.Value === equipmentId);
		if (selectequip) {
			//    stateEquipmentSerial.vmModel.Entity.EquipName=selectequip.Text;
		} else {
			//  stateEquipmentSerial.vmModel.Entity.EquipName='';
		}
	} else {
		// 父级未选中或被清空：重置子级状态
		stateEquipmentSerial.AllEquipTags = []; // 清空标签列表
		// stateEquipmentSerial.vmModel.Entity.EquipName='';
		stateEquipmentSerial.vmModel.Entity.TagId = null; // 清空标签选中值
	}
};

//选择地址时
const onEquipmentTagChange = (val: any) => {
	if (val) {
		// 从AllEquipTags中找到Value等于val的标签对象
		const selectedTag = stateEquipmentSerial.AllEquipTags.find((item) => item.Value === val);
		// 如果找到，将其Address赋值给EquipTagAddress
		if (selectedTag) {
			stateEquipmentSerial.vmModel.Entity.TagName = selectedTag.Text;
		} else {
			stateEquipmentSerial.vmModel.Entity.TagName = '';
		}
	} else {
		stateEquipmentSerial.vmModel.Entity.TagName = '';
	}
};

// 页面加载时
onMounted(() => {
	other.getSelectList('/api/EquipmentManagement/EquipmentSerial/GetEquipments', [], false).then((x) => {
		stateEquipmentSerial.AllEquipments = x;
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};
// 暴露变量
defineExpose({});
</script>

<style scoped lang="scss"></style>
